/**
 * Gets random integer inside range or snap to min/max values.
 */

export function randInt(min: number, max: number) {
  min = min == null ? Number.MIN_SAFE_INTEGER : ~~min
  max = max == null ? Number.MAX_SAFE_INTEGER : ~~max
  min -= 0.5
  max += 0.499999999999
  const r = min + (max - min) * Math.random()
  // can't be max + 0.5 otherwise it will round up if `rand`
  // returns `max` causing it to overflow range.
  // -0.5 and + 0.49 are required to avoid bias caused by rounding
  return Math.round(r)
}
